package com.newhopegroup.webcommons.restful.api.result;

import com.google.common.collect.ImmutableList;

import java.util.function.Predicate;

/**
 * Created by wangshida on 15/10/28.
 */
public enum StatusCode {
    DONE("0000", "DONE", "成功"),
    FAILED("9999", "FAILED", "失败");

    private String code;
    private String msg;
    private String desc;

    StatusCode(String code, String msg, String desc) {
        this.code = code;
        this.msg = msg;
        this.desc = desc;
    }

    public static StatusCode codeOf(final String code) {
        return xOf(code, statusCode -> statusCode.getCode().equals(code));
    }

    public static StatusCode msgOf(final String msg){
        return xOf(msg,statusCode -> statusCode.getMsg().equals(msg));
    }


    public static StatusCode xOf(final Object cond, final Predicate<StatusCode> predicate) {
        Object[] result = ImmutableList.copyOf(values()).parallelStream().filter(statusCode -> predicate.test(statusCode)).toArray();
        if (result.length == 1)
            return (StatusCode) result[0];
        throw new RuntimeException("no StatusCode be found by [" + cond + "]");
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return "StatusCode{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}
